package PersistanceLayer;

import BusinessLayer.UE;

import PersistanceLayer.DAO;

import java.beans.Statement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;

import java.sql.Connection;

import java.sql.SQLException;

import java.util.ArrayList;

import jdbc.ConnectionToOracle;

public class UEDAO extends DAO<UE> {

     private static final UEDAO instance = new UEDAO();

     /* Methode permettant de recuperer l'objet unique de type UEDAO
     */
    public final static UEDAO getInstance() {
        return instance;
    }

    @Override
    public boolean create(UE obj) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public boolean update(UE obj) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public boolean update(UE ancien, UE nouveau) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public UE find(UE obj) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public boolean delete(UE obj) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public ArrayList<UE> getListe() {
    	String codeUE;
    	String nbEcts;
    	String libUE;
    	String optionnel;
    	
    	ArrayList<UE> ensUe = new ArrayList<UE>();

    	
    	try {
    		
    		java.sql.Statement state =this.connect.createStatement();
    		
			ResultSet res = this.connect.createStatement()
			.executeQuery("SELECT * FROM UE");
		
			ResultSetMetaData resultMeta = res.getMetaData();
			
			while(res.next()){	
				if(res.getObject(1) != null)
					codeUE = res.getObject(1).toString();
				else
					codeUE = "";
				
				if(res.getObject(4) != null)
					nbEcts = res.getObject(4).toString();
				else
					nbEcts ="" ;
				
				if(res.getObject(5) != null)
					libUE = res.getObject(5).toString();
				else
					libUE = "";
				
				if(res.getObject(6) != null)
					optionnel = res.getObject(6).toString();
				else
					optionnel = "";
		
				ensUe.add(new UE(codeUE,nbEcts,libUE,optionnel));				
				
				
			}
			
			state.close();
			res.close();
			
			return ensUe;
    	} catch (SQLException e) {
			// 
    		
			e.printStackTrace();
			return null;
		}
    	
    }
    

    /**
     * Retourne l'ensemble des UE appartenant � un semestre donn�
     * @param name est le code du semestre dont on retourner l'ensemble
     * des UE
     */
	public ArrayList<UE> getListe(String name) {
		// TODO Auto-generated method stub
		ArrayList<UE> res = new ArrayList<UE>();
		
		String codeUe;
		String nbEcts;
		String libUe;
		try {
			
			//Cr�ation d'un objet Statement
			java.sql.Statement state = this.connect.createStatement();
			//L'objet ResultSet contient le r�sultat de la requ�te SQL
			ResultSet result = state.executeQuery("SELECT * " +
					"FROM UE " +
					"WHERE CODESEMESTRE = '"+name+"'");
			
			
			//On r�cup�re les MetaData
			ResultSetMetaData resultMeta = result.getMetaData();
			while(result.next())
			{
				
				if(result.getObject(1)!= null)
					codeUe = result.getObject(1).toString();
				else
					codeUe = "";
				
				if(result.getObject(4)!= null)
					nbEcts = result.getObject(4).toString();
				else
					nbEcts = "";
				
				if(result.getObject(5)!= null)
					libUe = result.getObject(5).toString();
				else
					libUe = "";
				
				res.add(new UE(codeUe, nbEcts, libUe,""));
			}
            result.close();
            state.close();

			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return res;
	}

	@Override
	public String getNom(String name) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean update(String a, String b, String c, String d) {
		// TODO Auto-generated method stub
		return false;
	}


    	


}